/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Core.CoreManager;
import DataAccess.TheLoai;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author My PC
 */
public class TheLoaiController extends CoreManager<TheLoai> {
    private String[] columnNames = {"Mã thể loại","Tên thể loại"};
     private String[] clmNames = {"TheLoaiD","TenTheLoai"};
    public TheLoaiController()
    { super();}
    @Override
    public void SetcolumnNames()
    {
        super.columnNames=columnNames;
    }
    public void showData(JTable table)
    {
        GetAllData("TheLoai");
        DefaultTableModel tableModel=new DefaultTableModel(columnNames, 0);
        table.setModel(tableModel);
       // int no=1;
        for(Object o: super.list)
        {
            TheLoai tg=(TheLoai)o;
            Vector oneRow=new Vector();
            oneRow.add(tg.getTheLoaiId());
            oneRow.add(tg.getTenTheLoai());
            tableModel.addRow(oneRow);
          //  System.out.println(tableModel.);
        }
    }
    public TheLoai getTheLoaiByIndex(int index)
    {
        return (TheLoai)list.get(index);
    }
    
      public int GetLastID()
    {
        if(list.size()!=0)
        return ((TheLoai)this.list.get(list.size()-1)).getTheLoaiId();
        else return 0;
    }
      public void SearchObject( String[] keywords,JTable table)
    {
        boolean flag=true;
        List<TheLoai> lstObj;
        String str="from TheLoai where ";
        for(int i=1;i<keywords.length;i++)
            {
                if(keywords[i].length()!=0)
                {
                    if(!flag) str+=" and ";
                    str+=clmNames[i]+"= :s"+String.valueOf(i);
                    flag=false;
                }
            }
        if(flag) str="from TheLoai";
         List<TheLoai> lst=null;
        try
        {
            this.session.beginTransaction();
            if(!flag)
            {     
        this.query=this.session.createQuery(str);
        for(int i=1;i<keywords.length;i++)
            {
                if(keywords[i].length()!=0)
                {
                   this.query.setParameter("s"+String.valueOf(i), keywords[i]);
                }
            }
            }
            else
         this.query=this.session.createQuery(str);
        System.out.println(query);
        this.session.getTransaction().commit();
        lst=this.query.list();
    //    System.out.println(lst);
          //  this.session.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
         DefaultTableModel tableModel=new DefaultTableModel(columnNames, 0);
        table.setModel(tableModel);
       // int no=1;
        for(Object o: lst)
        {
            TheLoai tg=(TheLoai)o;
            Vector oneRow=new Vector();
            oneRow.add(tg.getTheLoaiId());
            oneRow.add(tg.getTenTheLoai());
            tableModel.addRow(oneRow);
        } 
            }
}

